<?php

$data=[
    [
        "bo_id"=>16279,
        "goods_id"=>495869,
        "goods_name"=>"秒杀 CONVERSE/匡威 超特价经典款高帮帆布鞋惊喜价（绝版老标拍下不退）"
    ],
    [
        "bo_id"=>1612379,
        "goods_id"=>495812369,
        "goods_name"=>"3123布鞋惊喜价（绝版老标拍下不退）"
    ]
];

$title = array('晒单ID', '用户ID', '用户名', '商品ID', '商品名', '分类', '品牌', '供应商', '评论', '晒单时间', '赞数量');
$filename = '测试晒单列表_' . date('Ymd_His');
exportexcel($data, $title, $filename);


function exportexcel($data = [], $title = [], $filename = 'export',$strLine=array())
{
    header("Content-type:application/octet-stream");
    header("Accept-Ranges:bytes");
    header("Content-type:application/vnd.ms-excel");
    header("Content-Disposition:attachment;filename=" . $filename . ".xls");
    header("Pragma: no-cache");
    header("Expires: 0");
    echo '<html xmlns:o="urn:schemas-microsoft-com:office:office"
   xmlns:x="urn:schemas-microsoft-com:office:excel"
   xmlns="http://www.w3.org/TR/REC-html40">
<head>
   <meta http-equiv="expires" content="Mon, 06 Jan 1999 00:00:01 GMT">
   <meta http-equiv=Content-Type content="text/html; charset=gb2312">
</head>';
    //导出xls 开始
    echo '<table style="border: 1px solid #999;">';
    if (!empty($title)) {
        echo '<tr>';
        foreach ($title as $k => $v) {
            $title[$k] = '<td style="border: 1px solid #999;">'.iconv("UTF-8", "GBK", $v).'</td>';
        }
        //$title = implode("\t", $title);
        $title = implode("", $title);
        echo "$title\n";
        echo '</tr>';
    }
    if (!empty($data)) {
        foreach ($data as $key => $val) {
            $i=1;
            foreach ($val as $ck => $cv) {
                if(in_array($i,$strLine)){
                    $data[$key][$ck] = '<td style="border: 1px solid #999;vnd.ms-excel.numberformat:@">'.iconv("UTF-8", "GBK", $cv).'</td>';
                }else{
                    $data[$key][$ck] = '<td style="border: 1px solid #999;">'.iconv("UTF-8", "GBK", $cv).'</td>';
                }
                $i++;
            }
            //$data[$key] = implode("\t", $data[$key]);
            $data[$key] = '<tr>'.implode("", $data[$key]).'</tr>';

        }
//            echo implode("\n", $data);
        echo implode("", $data);
    }
    echo '</table>';

}